package user

import (
	"context"

	"mall/common/jwtx"
	"mall/service/backend/api/internal/svc"
	"mall/service/backend/api/internal/types"
	"mall/service/backend/rpc/backendclient"

	"github.com/zeromicro/go-zero/core/logx"
)

type RemoveUserLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

func NewRemoveUserLogic(ctx context.Context, svcCtx *svc.ServiceContext) *RemoveUserLogic {
	return &RemoveUserLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *RemoveUserLogic) RemoveUser(req *types.RemoveUserRequest) (resp *types.RemoveUserResponse, err error) {
	if len(req.Ids) == 0 {
		return &types.RemoveUserResponse{
			Status:  "ok",
			Message: "",
		}, nil
	}

	adminId, _ := jwtx.GetUserId(l.ctx)

	var force int32
	if req.Force {
		force = 1
	}
	_, err = l.svcCtx.BackendRpc.RemoveUser(l.ctx, &backendclient.RemoveUserRequest{
		Ids:       req.Ids,
		Force:     force,
		Status:    3,
		UpdatedBy: adminId,
	})
	if err != nil {
		return nil, err
	}

	return &types.RemoveUserResponse{
		Status:  "ok",
		Message: "删除用户成功",
	}, nil
}
